class User::TempController < ApplicationController

  def test
    @test=Survey.find(params[:id])
    @tempuser =TempUser.find(session[CURRENT_TEMPUSER_ID])
    @tempuser.tested(params[:id]) 
    if @test.t_type == TEST_TYPE_PROF
         if @test.has_part != 0
          @pages = @test.paging
            if @test.rg_method == "ruiwen"
              flash[:agenotice]="请准确填写被测者年龄，如：5岁填5，5岁半 填写5.5。每半岁一个年龄段。本量表适合于5到17岁"
            end
          render :template => "user/temp/prof_survey",:layout => false
        else
          @survey = @test.paging_no_part
           respond_to do |format|
            format.html {render :template => "user/temp/prof_survey_no_part",:layout => false}
            format.xml { render :xml => @test.to_xml }
           end
        end
    end
  end
  
  def welcome
    @user=TempUser.find(params[:id])
  end
  
  
  def prof_result
    @test = Survey.find(params[:survey_id])
    @tempuser =TempUser.find(session[CURRENT_TEMPUSER_ID])

    username = params[:username].strip == "" ? "匿名" : params[:username]
    @test_record = SurveyRecord.new
    @test_record.age=params[:age]
    @test_record.gender=params[:gender]
    @test_record.perfession=params[:perfession]
    @test_record.edu_status=params[:edu_status]
    @test_record.testname=@test.name
    @test_record.testtime=params[:timer]
    @test_record.username = username 
    @test_record.folder=@tempuser.folder
    @test_record.survey_id = params[:survey_id]
    @test_record.org = @tempuser.org
    @test_record.temp_user_name=@tempuser.name
    @test_record.save!
 
    #@tempuser.used=true
    @tempuser.record_id=@test_record.id
    @tempuser.save
    params[:question_id].each do |q_id|
      #收集每道题的分值／记录原始分
      @or = OriginalResult.new
      @or.survey_id=params[:survey_id]
      @or.question_id = q_id
      @or.part_id = params["question-#{q_id}-part"].to_i if @test.has_part
      @or.value = Float(params["question-#{q_id}-value"])
      @or.choice_num = params["question-#{q_id}-choice"]
      @or.test_record_id = @test_record.id
      @or.save
    end
 
    render :layout => false
  end
  
  
end
